<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
use Spatie\Permission\Models\Role;

class RoleRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        $request = request();
        $where = [
            'title' => $request->input('title'),
            'site_id' => site()['id'],
        ];
//        return [
//            'title'=>['required','between:3,50',Rule::unique('roles')->ignore(request()->role)],
//            'name'=>['required','between:3,50','regex:/^[\w\-]+$/',Rule::unique('roles')->ignore(request()->role)]
//        ];

        return [
            'title' => ['required', 'between:3,50', Rule::unique('roles')->where(function ($query) use ($where) {
                return $query->where($where);
            })->ignore($request->role)],
            'name' => ['required', Rule::unique('roles')->where(function ($query) use ($where) {
                return $query->where($where);
            })->ignore($request->role)]
        ];
    }

    public function messages()
    {
        return
            ['name.regex' => '请输入英文的角色'];
    }
}
